package com.leapp.sk.viewdelegate.notify

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v4.widget.SwipeRefreshLayout
import android.text.TextUtils
import android.view.View
import android.widget.LinearLayout
import com.example.inter.ListViewRefreshInterface
import com.example.inter.RefreshLoadMoreInferface
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.RefreshListView
import com.example.mvp.view.SKTopBar
import com.example.util.ISTECHER
import com.leapp.sk.R
import com.leapp.sk.bean.ClassNotifyEntiry
import com.leapp.sk.bean.ParentClassNotifyEntity
import com.leapp.sk.bean.TeacherClassNotifyEntity
import com.leapp.sk.ui.activity.notify.ClassNotifyActivity
import com.leapp.sk.ui.adapter.ClassNotifyParentAdapter
import com.leapp.sk.ui.adapter.ClassNotifyTeacherAdapter

/**
 * 班级通知
 * @author  chaohaipeng  on 2018/8/2.
 */
class ClassNotifyDelegate : BaseAppDelegate() {

    /**
     * 刷新接口
     */
    private var listViewRefreshInterface: ListViewRefreshInterface? = null

    /**
     * 家长端适配器
     */
   private var parentAdapter:ClassNotifyParentAdapter?=null
    /**
     * 老师端适配器
     */
    private var teacherAdapter:ClassNotifyTeacherAdapter?=null
    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_class_notify
    }

    /**
     * 初始化家长适配器
     */
    fun initParentAdapter(context: Context, mlist: List<ParentClassNotifyEntity>, refreshLoadMoreInferface: RefreshLoadMoreInferface, listener: SwipeRefreshLayout.OnRefreshListener) {
        val lv_acn_list = getView<RefreshListView>(R.id.lv_acn_list)
        val srl_acn_swipe = getView<SwipeRefreshLayout>(R.id.srl_acn_swipe)
        lv_acn_list.initFooterView(context, refreshLoadMoreInferface)
        listViewRefreshInterface = lv_acn_list.getListViewRefreshInterface()
        parentAdapter= ClassNotifyParentAdapter(getActivity(), mlist)
        lv_acn_list.adapter = parentAdapter
        srl_acn_swipe.setOnRefreshListener(listener)
        srl_acn_swipe.setColorSchemeColors(ContextCompat.getColor(context, R.color.color_19AD19))


    }

    /**
     * 设置发送按钮显示隐藏
     */
     fun initSendBtnVisible(isVisible:Boolean){
         val ll_asn_send_notify = getView<LinearLayout>(R.id.ll_asn_send_notify)
         if (isVisible) {
             ll_asn_send_notify.visibility = View.VISIBLE
         } else {
             ll_asn_send_notify.visibility = View.GONE
         }

     }


    /**
     * 刷新适配器
     */
    fun notifyAdapter(){
        parentAdapter?.notifyDataSetChanged()
        teacherAdapter?.notifyDataSetChanged()
    }

    /**
     * 设置加载数据
     */
    fun setLoadPageData(currentPage:Int,totalPage:Int,totalCount:Int){
        if (listViewRefreshInterface != null) {
            listViewRefreshInterface?.loadMore(currentPage, totalPage, totalCount)
        }

    }
    /**
     * 初始化适老师配器
     */
    fun initTeacherAdapter(context: Context, mlist: List<TeacherClassNotifyEntity>, refreshLoadMoreInferface: RefreshLoadMoreInferface, listener: SwipeRefreshLayout.OnRefreshListener) {
        val lv_acn_list = getView<RefreshListView>(R.id.lv_acn_list)
        val srl_acn_swipe = getView<SwipeRefreshLayout>(R.id.srl_acn_swipe)
        lv_acn_list.initFooterView(context, refreshLoadMoreInferface)
        listViewRefreshInterface = lv_acn_list.getListViewRefreshInterface()
        teacherAdapter = ClassNotifyTeacherAdapter(getActivity(), mlist)
        lv_acn_list.adapter = teacherAdapter
        srl_acn_swipe.setOnRefreshListener(listener)
        srl_acn_swipe.setColorSchemeColors(ContextCompat.getColor(context, R.color.color_19AD19))
        val ll_asn_send_notify = getView<LinearLayout>(R.id.ll_asn_send_notify)

    }




    /**
     * 停止刷新
     */
    fun stopSwipRefresh() {
        val srl_acn_swipe = getView<SwipeRefreshLayout>(R.id.srl_acn_swipe)
        srl_acn_swipe.isRefreshing = false
    }

    /**
     * 设置标题
     */
    fun setTitle(title: String?) {
        val topBar = getView<SKTopBar>(R.id.jtb_acn)
        if (!TextUtils.isEmpty(title)) {
            topBar?.setTitle(title)
        }
        setEvent(topBar)
    }

    /**
     * 设置设置事件
     */
    private fun setEvent(topBar: SKTopBar) {
        topBar?.setEventInterface(object : SKTopBar.EventInterface {
            override fun leftOnClick() {
                getActivity<ClassNotifyActivity>().finish()

            }

            override fun rightOnClick() {
            }

        })
    }
}